package com.lollipop.synthential.java8.lambda.stream;

import java.util.Arrays;
import java.util.List;
import java.util.stream.IntStream;
import java.util.stream.Stream;

/**
 * @Author: J.K
 * @Date: 2021-04-09 15:26
 * @Description: Java8的流式操作-Stream
 * 3中方式创建Stream：
 * 1.通过java.util.Collection.stream()方法用集合创建流
 * 2.通过java.util.Arrays.stream(T[] array)方法用数组创建流
 * 3.通过Stream的静态方法:of(),iterate(),generate()
 */
public class StreamDemo1 {

    /**
     * 1.通过java.util.Collection.stream()方法用集合创建流
     */
    void createStreamByCollection() {
        List<String> list = Arrays.asList("1,2,6,a");
        // 创建一个顺序流
        Stream<String> stream = list.stream();
        stream.forEach(System.out::println);

        // 创建一个并行流
        Stream<String> parallelStream = list.parallelStream();
        parallelStream.forEach(System.out::println);
    }

    /**
     * 2.通过java.util.Arrays.stream(T[] array)方法用数组创建流
     */
    void createStreamByArray() {
        int[] array = {123, 5, 6};
        IntStream stream = Arrays.stream(array);
        stream.forEach(System.out::println);
    }

    /**
     * 3.通过Stream的静态方法:of(),iterate(),generate()
     */
    void createStreamBySataticMethod() {
        Stream<Integer> integerStream = Stream.of(1, 2, 6);
        integerStream.forEach(System.out::println);

        Stream<Integer> limitStream = Stream.iterate(0, (x) -> x + 3).limit(2);
        limitStream.forEach(System.out::println);

        Stream<Double> doubleStream = Stream.generate(Math::random).limit(3);
        doubleStream.forEach(System.out::println);
    }

}
